package nl.biz_e.appengine.gar.client;

import nl.biz_e.appengine.gar.client.i18n.Constants;
import nl.biz_e.appengine.gar.client.ui.GarPanel;
import nl.biz_e.appengine.gar.client.ui.LoginPanel;
import nl.biz_e.appengine.gar.shared.User;
import nl.biz_e.gwt.baselib.ui.client.menu.IMenuItem;
import nl.biz_e.gwt.baselib.ui.client.menu.MenuItem;
import nl.biz_e.gwtappengine.lib.client.AbstractApplication;
import nl.biz_e.gwtappengine.lib.client.panels.WelcomePanel;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;

/**
 * 
 */
public class GastouderRegistratie extends AbstractApplication {
    private final LoginItem loginLink;
    private User currentUser = null;

    /**
     * Constructs a new GastouderRegistratie instance
     */
    public GastouderRegistratie() {
        super(Constants.getGarConstants().appname());
        loginLink = new LoginItem(this);
        this.setUseGoogleLogin(false);
        this.addMenuItem(new PopupMenuItem());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onStart() {
        showPanel(new WelcomePanel(this.getAppName()));
    }

    /**
     * @param currentUser
     *            the currentUser to set
     */
    public void setCurrentUser(User currentUser) {
        this.currentUser = currentUser;
        this.getPanelManager().getMenuManager().setUserName(
                currentUser.getName());
    }

    /**
     * @return the currentUser
     */
    public User getCurrentUser() {
        return currentUser;
    }

    void logIn() {
        this.showPanel(new LoginPanel());
    }

    void logOut() {
        this.loggedOut();
    }

    /**
     * 
     */
    public void loggedIn() {
        loginLink.setLoggedIn(true);
        this.getPanelManager().getMenuManager().setLoggedIn(true);
        this.showPanel(new GarPanel(this));
    }

    /**
     * @param user
     */
    public void loggedIn(User user) {
        this.setCurrentUser(user);
        this.loggedIn();
    }

    /**
     * 
     */
    public void loggedOut() {
        loginLink.setLoggedIn(false);
        this.getPanelManager().getMenuManager().setLoggedIn(false);
        this.showPanel(new WelcomePanel(getAppName()));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public IMenuItem getLoginItem() {
        return this.loginLink;
    }

    private static class LoginItem extends MenuItem {
        private final GastouderRegistratie app;
        boolean loggedIn = false;
        private static String LOGIN = Constants.getGarConstants().logIn();
        private static String LOGOUT = Constants.getGarConstants().logOut();

        public LoginItem(GastouderRegistratie app) {
            super(LOGIN);
            this.app = app;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public void onClick(ClickEvent event) {
            if (this.loggedIn) {
                app.logOut();
            } else {
                app.logIn();
            }
        }

        void setLoggedIn(boolean loggedIn) {
            if (this.loggedIn == loggedIn)
                return;
            this.loggedIn = loggedIn;
            if (loggedIn) {
                this.setText(LOGOUT);
            } else {
                this.setText(LOGIN);
            }
        }

    }

    private static class PopupMenuItem extends MenuItem {
        public PopupMenuItem() {
            super("Popup", new MenuItemClickedListener() {

                @Override
                public void menuItemClicked(IMenuItem item) {
                    new MyPopup().show();

                }
            }, true);
        }
    }

    private static class MyPopup extends PopupPanel {

        public MyPopup() {
            // PopupPanel's constructor takes 'auto-hide' as its boolean
            // parameter.
            // If this is set, the panel closes itself automatically when the
            // user
            // clicks outside of it.
            super(true);

            // PopupPanel is a SimplePanel, so you have to set it's widget
            // property to
            // whatever you want its contents to be.
            setWidget(new Label("Click outside of this popup to close it"));
        }
    }

}
